package com.banker.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.banker.entity.ToAllot;
import com.banker.service.ToAllotService;
import com.banker.web.query.ToAllotQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;

/**
 * bank 接口
 */
@Controller
public class ToAllotController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/to_allot/bank";


    @Autowired private ToAllotService bankService;
    
    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("to_allot.bank.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/to_allot/bank/index.html") ;
        view.addObject("search", ToAllotQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("to_allot.bank.edit")
    @ResponseBody
    public ModelAndView edit(Integer id) {
        ModelAndView view = new ModelAndView("/to_allot/bank/edit.html");
        ToAllot bank = bankService.queryById(id);
        view.addObject("bank", bank);
        return view;
    }

    @GetMapping(MODEL + "/add.do")
    @Function("to_allot.bank.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/to_allot/bank/add.html");
        return view;
    }

    /* ajax json */

    @PostMapping(MODEL + "/list.json")
    @Function("to_allot.bank.query")
    @ResponseBody
    public JsonResult<PageQuery> list(ToAllotQuery condtion)
    {
        PageQuery page = condtion.getPageQuery();
        bankService.queryByCondition(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/add.json")
    @Function("to_allot.bank.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)ToAllot bank)
    {
        bankService.save(bank);
        return new JsonResult().success();
    }

    @PostMapping(MODEL + "/edit.json")
    @Function("to_allot.bank.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  ToAllot bank) {
        boolean success = bankService.update(bank);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }


   
    @GetMapping(MODEL + "/view.json")
    @Function("to_allot.bank.query")
    @ResponseBody
    public JsonResult<ToAllot>queryInfo(Integer id) {
        ToAllot bank = bankService.queryById( id);
        return  JsonResult.success(bank);
    }

    @PostMapping(MODEL + "/delete.json")
    @Function("to_allot.bank.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        bankService.batchDelToAllot(idList);
        return new JsonResult().success();
    }
    

}
